<?php

$auth = @$_SESSION['auth'];
if (empty($auth)) die('用户未登录！^_^');

if (!check_account_addr($auth['id'])) die('e1');

$topic_id = intval($_GET['topic_id']);

$cond = "WHERE user_id = ".$auth['id']." AND topic_id = $topic_id";

if (!dt_query_one("SELECT id FROM forum_topic_order $cond LIMIT 1")) {
	$rs = dt_query("INSERT INTO forum_topic_order (user_id, topic_id, c_at) VALUES (".$auth['id'].", $topic_id, ".time().")");
	if (!$rs) die('新增forum_topic_order数据失败！');
	$rs = dt_query("UPDATE forum_topic SET order_c = order_c + 1, order_c_s = order_c_s + 1 WHERE id = $topic_id");
	if (!$rs) die('统计forum_topic数据失败！');
	die('s0');
} else {
	$rs = dt_query("DELETE FROM forum_topic_order $cond");
	if (!$rs) die('删除forum_topic_order数据失败！');
	$rs = dt_query("UPDATE forum_topic SET order_c = CASE WHEN order_c > 0 THEN order_c - 1 END, order_c_s = order_c_s - 1 WHERE id = $topic_id");
	if (!$rs) die('统计forum_topic数据失败！');
	die('s1');
}
